package day_2022_9_to_12.Day;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author haomin
 * @date 2022/10/10 17:57
 **/
public class Day38_3_Num205_IsIsomorphic {
    // 错误并不是换位
    class Solution2 {
        public boolean isIsomorphic(String s, String t) {
            if(s.length() != t.length()) return false;
            int[] arr1 = new int[26];
            int[] arr2 = new int[26];
            for(int i = 0; i < s.length(); ++i){
                arr1[s.charAt(i) - 'a']++;
                arr2[t.charAt(i) - 'a']++;
            }
            Arrays.sort(arr1);
            Arrays.sort(arr2);
            return Arrays.equals(arr1, arr2);
        }
    }
    class Solution3 {
        public boolean isIsomorphic(String s, String t) {
            if(s.length() != t.length()) return false;
            Map<Character, Character> map = new HashMap<>();
            for(int i = 0; i < s.length(); ++i){
                char a = s.charAt(i);
                char b = t.charAt(i);
                if(map.containsKey(a)){
                    if(!map.get(a).equals(b)) return false;
                }else{
                    map.put(a, b);
                }
            }
            return true;
        }
    }

    class Solution {
        public boolean isIsomorphic(String s, String t) {
            if(s.length() != t.length()) return false;
            Map<Character, Character> map = new HashMap<>();
            for(int i = 0; i < s.length(); ++i){
                char a = s.charAt(i);
                char b = t.charAt(i);
                if(!map.containsKey(a)) {
                    if(map.containsValue(b)) return false;
                    map.put(a, b);
                }else{
                    if(map.get(a) != b) return false;
                }
            }
            return true;
        }
    }
}